/**
 * Created by judasnow on 21/06/2017.
 */

import Vue from 'vue'
import VueResource from 'vue-resource'
import { API_ROOT, AUTH_COOKIE_NAME } from '../config'
import { getCookie } from '../util/auth'


Vue.use(VueResource);

// Vue.http.options.crossOrigin = true
Vue.http.options.xhr = {withCredentials: true};
Vue.http.options.emulateJSON = true;
Vue.http.options.emulateHTTP = true;

Vue.http.interceptors.push({
    request (request) {
        request.headers = request.headers || {}
        let token = getCookie(AUTH_COOKIE_NAME)

        if (token) {
            //    request.headers.Authorization = 'Bearer ' + token.replace(/(^\")|(\"$)/g, "")
            request.headers.custom = {
                "X-Access-Token": token.replace(/(^\")|(\"$)/g)
            }
        }

        return request
    },
    response (response) {
        return response
    }
});

export const resource = Vue.resource;
